<?php


class QrimgController extends ControllerBase
{
        public function listAction(){
            $pageNum = $this->request->getQuery("pageNum",null,1) ;
            $size = 50;
            $limit = ($pageNum-1)*50;
            $name = $this->request->get("name");
            $where = "";
            if($name){
                $where .=" name like '{%$name%}'";
            }
            $list = ImgInfo::find([
                "conditions"=>$where,
                "limit"=>$size,
                "order"=>"create_at desc ",
                "offset"=>$limit
            ]);
            $data = [];
            foreach ($list as $key=>$vo){
                $data[$key]=$vo->toArray();
                $data[$key]['img_path']=$vo->oss_path;
            }
            $total  = ImgInfo::count($where);
            $allPageNum = ceil($total/$size);
            $this->exitMessage(true, '', ["data"=>$data,"pageInfo"=>["size"=>$size,"current"=>$pageNum,"allPageNum"=>$allPageNum,"total"=>$total]]);
        }

        public function saveAction(){
            $name = $this->request->get("name");
            $wxh_id = $this->request->get("wxh_id");
            $dir_path = PUBLIC_PATH."/files/".date("Ymd");
            if(!is_dir($dir_path)){
                mkdir($dir_path,0777,1);
            }
            $aliyun = new Aliyun();
            if ($this->request->hasFiles()) {
                $files = $this->request->getUploadedFiles();

                // Print the real file names and sizes
                foreach ($files as $file) {
                    // Print file details
                    // echo $file->getName(), " ", $file->getSize(), "\n";

                    // Move the file into the application
                    $file->moveTo(
                        $dir_path."/". time().$file->getName()
                    );
                    $img = new ImgInfo();
                    $img->name = $name;
                    $img->wxh_id = $wxh_id;
                    $img->img_path = "/files/".date("Ymd")."/".time(). $file->getName();
                    $img->status = 1;

                    $img->create_at = date("Y-m-d H:i:s");

                    $res = $img->save();
                    if(!$res){
                        $this->exitMessage(false,"保存失败");
                    }
                }
            }
            $img = ImgInfo::findFirst($img->id);
            $res = $aliyun->upload(time()."_".$img->id.".png",PUBLIC_PATH.$img->img_path);
            $img->oss_path = $res['info']['url'];
            $img->save();

            $this->exitMessage(true,"上传成功");
        }


        public function editAction(){
            $id = $this->request->get("id");
            $name = $this->request->get("name");
            $status = $this->request->get("status");
            $dir_path = PUBLIC_PATH."/files/".date("Ymd");
            if(!is_dir($dir_path)){
                mkdir($dir_path,0777,1);
            }
            $img = ImgInfo::findFirst($id);
            $img->name = $name;
            if ($this->request->hasFiles()) {
                $files = $this->request->getUploadedFiles();

                // Print the real file names and sizes
                foreach ($files as $file) {
                    // Print file details
                    // echo $file->getName(), " ", $file->getSize(), "\n";

                    // Move the file into the application
                    $file->moveTo(
                        $dir_path."/". $file->getName()
                    );

                    $img->img_path = $dir_path."/". $file->getName();

                }
            }
            $img->status = $status;
            $img->update_at = date("Y-m-d H:i:s");
            $res = $img->save();
            if(!$res){
                foreach ($img->getMessages() as $message){
                    echo $message->getMessage();die;
                }
                $this->exitMessage(false,"保存失败");
            }
            $this->exitMessage(true,"保存成功");
        }

        public function delAction(){
            $id = $this->request->get("id");
            if(!$id){
                $this->exitMessage(false,"缺少必要参数");
            }
            $info = ImgInfo::findFirst($id);
            $res = $info->delete();
            if(!$res){
                $this->exitMessage(false,"删除失败");
            }
            $this->exitMessage(true,"删除成功");
        }

        public function allAction(){
            $list = ImgInfo::find("status=1");
            $data= [];
            foreach ($list as $key=>$vo){
                $data[$key]=$vo->toArray();
                $data[$key]['img_path']="http://www.drinkt.org.cn".$vo->img_path;
            }
            $this->exitMessage(true,"",$data);
        }

}